package com.story.web.controllers;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.story.domain.Admin;
import com.story.domain.Page;
import com.story.service.IAdminService;
import com.story.web.utils.WebUtils;

@Controller
@RequestMapping("/manager/admin")
public class AdminManagerContorller {
	@Autowired
	private IAdminService adminService;

	// 分页展示Admin数据
	@RequestMapping("/page/show")
	@ResponseBody
	public Object showAdmins(Integer pageNum, Integer pageSize) {
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		if (pageNum == null || pageSize == null) {
			return null;
		}
		Page page = new Page(pageNum, pageSize);
		Integer total = adminService.getTotal();
		List<Admin> adminList = adminService.findAdminsByLimit(page);
		map.put("code", 0);
		map.put("msg", "查询成功！");
		map.put("count", total);
		map.put("data", adminList);
		return map;
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addAdmin(@Validated Admin admin,
			BindingResult result, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (result.hasFieldErrors()) {
			List<FieldError> errors = result.getFieldErrors();
			for (int i = 0; i < errors.size(); i++) {
				map.put("status", 1);
				map.put("msg", errors.get(i).getDefaultMessage());
			}
			return map;
		}

		Admin initAdmin = WebUtils.init(request, admin);
		adminService.addAdmin(initAdmin);
		map.put("status", 0);
		return map;
	}
	@RequestMapping("/{id}/delete")
	@ResponseBody
	public Map<String,Object> deleteAdmin(@PathVariable("id") String id){
		Map<String, Object> map = new HashMap<String, Object>();
		if(id == null || id.trim().equals("")){
			map.put("status", 1);
			map.put("msg", "删除失败，您还没有选择数据！");
			return map;
		}
		Subject subject = SecurityUtils.getSubject();
		String username = (String) subject.getPrincipal();
		Admin admin = adminService.findOneByUsername(username);
		boolean isCurrentSubject = WebUtils.isCurrentSuject(admin, id);
		if(isCurrentSubject){
			map.put("status", 1);
			map.put("msg", "哎哟喂,不能删除自己哟！");
			return map;
		}
		Integer num = adminService.deleteOneById(id);
		if(num>0){
			map.put("status", 0);
			map.put("msg", "删除成功！");
		}else{
			map.put("status", 1);
			map.put("1", "服务器发生异常,删除失败！");
		}
		return map;			
	}
	
	@RequestMapping("/{ids}/batch/delete")
	@ResponseBody
	public Map<String,Object> deleteAdmins(@PathVariable("ids")String[] ids){
		Map<String, Object> map = new HashMap<String, Object>();
		
		return map;		
	}
}
